﻿using System;
using System.Collections.Generic;
using System.Text;
using System.ComponentModel;
using System.Management.Automation;
using PowerOBIProvider;
using PowerOBIProvider.OBI;
using PowerOBIPRovider.OBIWebReference;
using System.Collections.ObjectModel;

namespace PowerOBIProvider.Cmdlts
{
    [Cmdlet(VerbsData.Compare, "Item")]
    public class CompareItemCmdlt : BaseOBICmdlt
    {
        private string source = null;
        [Parameter(Mandatory = true, Position = 1)]
        [ValidateNotNullOrEmpty]
        public string Source
        {
            get
            {
                return source;
            }
            set
            {
                source = value;
            }
        }
        private string target = null;
        [Parameter(Mandatory = true, Position = 2)]
        [ValidateNotNullOrEmpty]
        public string Target
        {
            get
            {
                return target;
            }
            set
            {
                target = value;
            }
        }

        protected override void ProcessRecord()
         {
             PathInfo sourcePathInfo = GetPathInfoFromPath(source);
             PSOBIDriveInfo sourceDrive = GetOBIDrive(sourcePathInfo);

             PathInfo targetPathInfo = GetPathInfoFromPath(target);
             PSOBIDriveInfo targetDrive = GetOBIDrive(targetPathInfo);
             if (sourceDrive != null && targetDrive != null)
             {
                 try
                 {
                     CatalogObject sourceCatalogObject = sourceDrive.ObiWSClient.ReadItem(sourcePathInfo.ProviderPath);
                     CatalogObject targetCatalogObject = targetDrive.ObiWSClient.ReadItem(targetPathInfo.ProviderPath);
                     if (sourceCatalogObject.itemInfo.type == ItemInfoType.Folder ||
                         targetCatalogObject.itemInfo.type == ItemInfoType.Folder)
                         throw new Exception("Cannot compare folders.");
                     else if (sourceCatalogObject.itemInfo.type == ItemInfoType.NoAccess ||
                         targetCatalogObject.itemInfo.type == ItemInfoType.NoAccess)
                         throw new Exception("Cannot read object - read permissions missing");
                     else if (sourceCatalogObject.itemInfo.type == ItemInfoType.Link||
                         targetCatalogObject.itemInfo.type == ItemInfoType.Link)
                         throw new Exception("Cannot compare links");
                     else
                     {
                         string sourceObjectCatalog = sourceCatalogObject.catalogObject;
                         string targetObjectCatalog = targetCatalogObject.catalogObject;
                         if (sourceObjectCatalog == targetObjectCatalog)
                             WriteObject(true);
                         else
                             WriteObject(false);
                     }
                 }
                 catch (Exception ex)
                 {
                     WriteError(new ErrorRecord(ex, "0", ErrorCategory.OperationStopped, null));
                 }
             }
             else
             {
                 WriteError(new ErrorRecord(new Exception("Source or target path does not exist"),"0",ErrorCategory.InvalidArgument,null) );
             }
         }
    }
}
